package pl.gettoknowme.mobile.android.services.location;

import pl.gettoknowme.mobile.android.util.LoggingUtil;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Odbiorca komunikatów o żądaniu pobrania lokalizacji
 * @author Michał Kołodziejski
 */
public class LocationAlarmReceiver extends BroadcastReceiver {

    public static final String GET_LOCATION_ACTION = 
        "pl.gettoknowme.mobile.android.services.location.GET_LOCATION";

    public static final String START_FETCHING_LOCATIONS_ACTION = 
        "pl.gettoknowme.mobile.android.services.location.START_FETCHING_LOCATIONS";

    public static final String STOP_FETCHING_LOCATIONS_ACTION = 
        "pl.gettoknowme.mobile.android.services.location.STOP_FETCHING_LOCATIONS";
    
    /**
     * Interwał pomiędzy kolejnymi wywołaniami pobrania lokalizacji.
     * 10 min.
     */
    private static long LOCATION_FETCH_INTERVAL = 1000l * 60l * 10l;
    

    @Override
    public void onReceive(Context context, Intent intent) {
        if(GET_LOCATION_ACTION.equals(intent.getAction())){
            LoggingUtil.d(this, "GET_LOCATION_ACTION catched - starting getting current location");
            getLocation(context);
            
        } else if(START_FETCHING_LOCATIONS_ACTION.equals(intent.getAction())){
            LoggingUtil.d(this, "START_FETCHING_LOCATIONS_ACTION catched - starting alarm manager to fetch locations");
            setupAlarmManager(context);
            
        } else if(STOP_FETCHING_LOCATIONS_ACTION.equals(intent.getAction())){
            LoggingUtil.d(this, "STOP_FETCHING_LOCATIONS_ACTION catched - stopping fetching locations");
            stopFetchingLocations(context);
            
        } else if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
            LoggingUtil.d(this, "ACTION_BOOT_COMPLETED catched - starting alarm manager to fetch locations");
            setupAlarmManager(context);
            
        }
    }
    
    
    public static boolean isFetchingLocationsRunning(Context context){
        Intent intent = new Intent(GET_LOCATION_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
                PendingIntent.FLAG_NO_CREATE);
        return (pendingIntent != null);
    }
    
    /**
     * Uruchamia serwis pobierający bieżącą lokalizację
     * @param context kontekst działania
     */
    private void getLocation(Context context){
        LoggingUtil.d(this, "Starting LocationService");
        Intent intent = new Intent(LocationService.START_LOCATION_SERVICE_ACTION);
        context.startService(intent);
    }
    
    /**
     * Startuje okresowe uruchamianie pobierania lokalizacji
     * @param context kontekst działania
     */
    private void setupAlarmManager(Context context){
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        
        PendingIntent pendingIntent = createGetLocationPendingIntent(context);
        long firstTrigger = System.currentTimeMillis() + 1000l * 5l; // za 5 sek.
        
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTrigger, LOCATION_FETCH_INTERVAL, pendingIntent);
    }
    
    
    /**
     * Zatrzymuje okresowe uruchamianie pobierania lokalizacji
     * @param context kontekst działania
     */
    private void stopFetchingLocations(Context context){
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        PendingIntent pendingIntent = createGetLocationPendingIntent(context);
        alarmManager.cancel(pendingIntent);
        pendingIntent.cancel();
    }

    /**
     * Tworzy PendingIntent powiadamiający o rozpoczęciu pobierania lokalizacji
     * @param context kontekst działania
     * @return PendingIntent powiadamiający o rozpoczęciu pobierania lokalizacji
     */
    private PendingIntent createGetLocationPendingIntent(Context context){
        Intent intent = new Intent(GET_LOCATION_ACTION);
        return PendingIntent.getBroadcast(context, 0, intent, 
                PendingIntent.FLAG_UPDATE_CURRENT);
    }
}
